#!/bin/bash

##############PARAMETROS CONFIGURABLES#######
CC="gcc"
C_ARGS="-O0 -g"
WORK_DIR="/tmp"
MODULO1_SRC="./tam_bloque.c"
MODULO2_SRC="./tam_cache.c"
MODULO3_SRC="./vias.c"
CACHE_PARAMETERS="8192,4,64"

##############NO TOCAR#######################
MODULO1=`tempfile`
MODULO2=`tempfile`
MODULO3=`tempfile`
VALGRIND_OUTPUT=`tempfile`
TEST_FUNCTION="read1M"
MAGIC_LABEL="MAGIC:"

##############FUNCIONES######################
VALGRIND_CALLER(){
valgrind --tool=cachegrind --D1="$CACHE_PARAMETERS" --branch-sim=no  --cachegrind-out-file="$VALGRIND_OUTPUT" $@ 2>/dev/null
}

EXIT_FUNCTION(){
rm $MODULO1 $MODULO2 $MODULO3 $VALGRIND_OUTPUT
exit $1
}
###############PARAMETROS####################
if [ $1 = "-v" ]
then echo CacheSuite V1.0
EXIT_FUNCTION 0
fi

if [ $1 = "-h" ]
then 
echo Help
echo Ejecutar sin parámetros para obtener información acerca de la caché.
echo Para cambiar los parametros de simulacion, 
echo modifique las variables al comienzo de este script.
EXIT_FUNCTION 0
fi
##############VERIFICA QUE ESTEN LAS FUENTES#

if [ ! -f $MODULO1_SRC ]; then echo falta $MODULO1_SRC; EXIT_FUNCTION 1; fi
if [ ! -f $MODULO2_SRC ]; then echo falta $MODULO2_SRC; EXIT_FUNCTION 1; fi
if [ ! -f $MODULO3_SRC ]; then echo falta $MODULO3_SRC; EXIT_FUNCTION 1; fi

##############COMPILACION#######################

$CC $C_ARGS $MODULO1_SRC -o $MODULO1
$CC $C_ARGS $MODULO2_SRC -o $MODULO2
$CC $C_ARGS $MODULO3_SRC -o $MODULO3

#############CORRE MODULO1#######################
MAGIC_LINE=$(grep -ne "^MAGIC:" $MODULO1_SRC  | cut -d : -f 1)
VALGRIND_CALLER $MODULO1
TEST_FUNCTION_BEGIN=$(cat $VALGRIND_OUTPUT | grep -n "fn=""$TEST_FUNCTION" | cut -d : -f 1)
MISSES_LINE=$(tail $VALGRIND_OUTPUT -n +"$TEST_FUNCTION_BEGIN" | grep -e "^""$MAGIC_LINE" | head -n 1 )
let NBLOQUES=$(echo $MISSES_LINE | cut -f 5 -d \  )/$(echo $MISSES_LINE | cut -f 6 -d \  )

#############CORRE MODULO2#######################
MAGIC_LINE=$(grep -ne "^MAGIC:" $MODULO2_SRC  | cut -d : -f 1)
VALGRIND_CALLER $MODULO2 $NBLOQUES
TEST_FUNCTION_BEGIN=$(cat $VALGRIND_OUTPUT | grep -n "fn=""$TEST_FUNCTION" | cut -d : -f 1)
MISSES_LINE=$(tail $VALGRIND_OUTPUT -n +"$TEST_FUNCTION_BEGIN" | grep -e "^""$MAGIC_LINE" | head -n 1 )
let NON_MISSED_ITERATIONS=$(echo $MISSES_LINE | cut -f 5 -d \  )-$(echo $MISSES_LINE | cut -f 6 -d \  )
let CACHE_SIZE="(""1<<(""$NON_MISSED_ITERATIONS""-1)"")*""$NBLOQUES"

#############CORRE MODULO3#######################
MAGIC_LINE=$(grep -ne "^MAGIC:" $MODULO3_SRC  | cut -d : -f 1)
let VIAS_GUESS_LIMIT="$NON_MISSED_ITERATIONS"-1
let VIAS_GUESS=$VIAS_GUESS_LIMIT
while [ $VIAS_GUESS -ne 0 ]
do
	VALGRIND_CALLER $MODULO3 $NBLOQUES $VIAS_GUESS_LIMIT $VIAS_GUESS
	TEST_FUNCTION_BEGIN=$(cat $VALGRIND_OUTPUT | grep -n "fn=""$TEST_FUNCTION" | cut -d : -f 1)
	MISSES_LINE=$(tail $VALGRIND_OUTPUT -n +"$TEST_FUNCTION_BEGIN" | grep -e "^""$MAGIC_LINE" | head -n 1 )
#	echo $VIAS_GUESS : $MISSES_LINE
	if [ $(($(echo $MISSES_LINE | cut -f 6 -d \ ) / $(echo $MISSES_LINE | cut -f 5 -d \ ))) -ne 0 ]
		then break
	fi
	let VIAS_GUESS--
done
let ASSOCIATIVITY="1<<$VIAS_GUESS"

echo "Memoria Caché de Datos Analizada:"
echo "#Vías: $ASSOCIATIVITY Vías"
echo "Tamaño Total: $CACHE_SIZE Bytes"
echo "Tamaño de Bloque: $NBLOQUES Bytes"




EXIT_FUNCTION 0
